/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAOInterface.DAOConnection;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Fabian
 */
public class DBMySQLConnection implements DAOConnection {

    /**
     * 
     */
    public DatabaseMetaData modeloBD;
    /**
     * 
     */
    public Connection conexion = null;
    /**
     * 
     */
    public Statement un_st = null;
    /**
     * 
     */
    public String s_conexion = null;
    private String host;
    private String database;
    private String username;
    private String password;
    private String lastQuery;

    /**
     * 
     * @return
     */
    @Override
    public DatabaseMetaData getModel() {
        return modeloBD;
    }

    /**
     * 
     * @param path
     * @param dbName
     * @param user
     * @param pass
     * @return
     */
    @Override
    public boolean setConnection(String path, String dbName, String user, String pass) {
        host = path;
        database = dbName;
        username = user;
        password = pass;
        if (openConnection()) {
            //closeConnection();
            return true;
        }
        return false;
    }

    /**
     * 
     * @return
     */
    @Override
    public boolean openConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conexion = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, username, password);
            modeloBD = conexion.getMetaData();
            un_st = conexion.createStatement();
           
            return true;
        } catch (SQLException ex) {
       
            Logger.getLogger(DBMySQLConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (ClassNotFoundException ex) {
           
            Logger.getLogger(DBMySQLConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    /**
     * 
     */
    @Override
    public void closeConnection() {
        try {
            conexion.close();
            
        } catch (SQLException ex) {
            Logger.getLogger(DBMySQLConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 
     * @param tabla
     * @param columna
     * @return
     */
    @Override
    public ResultSet getColumn(String tabla, String columna) {
        ResultSet rs = null;
        
        try {
            rs = un_st.executeQuery("select " + columna + "from " + tabla);
        } catch (SQLException ex) {
            Logger.getLogger(DBSQLserverConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return rs;
    }

    /**
     * 
     * @param tabla
     * @param columnas
     * @param parametros
     */
    @Override
    public void createElement(String tabla, ArrayList columnas, ArrayList parametros) {
        String query = "INSERT INTO " + tabla + " (";
        int largoP = parametros.size();
        int largoC = columnas.size();

        for (int i = 0; i < largoC - 1; i++) {
            query = query + columnas.get(i) + ", ";
        }
        query = query + columnas.get(largoC - 1) + ") VALUES ('";

        for (int i = 0; i < largoP - 1; i++) {
            query = query + parametros.get(i) + "', '";
        }
        query = query + parametros.get(largoP - 1) + "')";

        
        try {
            this.un_st.executeUpdate(query);
            lastQuery = "Motor MySQL: consulta '" + query + "' generada con exito";
        } catch (SQLException ex) {
            Logger.getLogger(DBPostGreSQLConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * 
     * @param tabla
     * @param columnas
     * @param filtro
     * @return
     */
    @Override
    public ResultSet getElement(String tabla, ArrayList columnas, String filtro) {
        ResultSet rs = null;
        String query = "SELECT ";
        int largoC = columnas.size();

        for (int i = 0; i < largoC - 1; i++) {
            query = query + columnas.get(i) + ", ";
        }
        if(filtro == null){
            query = query + columnas.get(largoC - 1) + " FROM " + tabla;
        }else{
            query = query + columnas.get(largoC - 1) + " FROM " + tabla + " WHERE " + filtro;
        }
        

        try {
            rs = this.un_st.executeQuery(query);
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(DBPostGreSQLConnection.class.getName()).log(Level.SEVERE, null, ex);
            return rs;
        }
        
    }

    /**
     * 
     * @param tabla
     * @return
     */
    @Override
    public String getPK(String tabla) {
        String pk = null;
        try {
            ResultSet rs = modeloBD.getPrimaryKeys(null, null, tabla);
            while (rs.next()) {
                pk = rs.getString(4);
//                System.out.println("pk de " + tabla + ": "+ pk);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBMySQLConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pk;
    }

    /**
     * 
     * @return
     */
    @Override
    public String getLastQuery() {
        return this.lastQuery;
    }
}
